package com.mobvoi.serialport.util;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;

public class Parser {

    public static String checkXor(String data) {
        int checkData = 0;
        for (int i = 0; i < data.length(); i = i + 2) {
            int start = Integer.parseInt(data.substring(i, i + 2), 16);
            checkData = start ^ checkData;
        }
        String ss = Integer.toHexString(checkData);
        if (ss.length() % 2 != 0) {
            ss = "0" + ss;
        }
        return ss.toUpperCase();
    }


    public static String byte2Hex(Byte inByte) {
        return String.format("%02x", inByte).toUpperCase();
    }

    public static String byteArrToHex(byte[] inBytArr, int len) {
        StringBuilder strBuilder = new StringBuilder();

        for (int i = 0; i < len; ++i) {
            strBuilder.append(byte2Hex(inBytArr[i]));
            strBuilder.append("");
        }

        return strBuilder.toString();
    }

    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                int parseInt = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
                if (parseInt == 0) {
                    baKeyword[i] = 32;
                } else {
                    baKeyword[i] = (byte) (0xff & parseInt);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, StandardCharsets.UTF_8);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    public static byte[] string2BH(String res) {
        byte[] bytes = res.getBytes();
        byte[] byte1 = new byte[4 + bytes.length];
        byte1[byte1.length - 1] = (byte) bytes.length;

        for (int i = 0; i < bytes.length; ++i) {
            byte1[byte1.length - 1] ^= bytes[i];
            byte1[i + 3] = bytes[i];
        }
        byte1[0] = -86;
        byte1[1] = 84;
        byte1[2] = (byte) bytes.length;
        return byte1;
    }

    public static String hexString2BH(String res) {
        res = res + "00";
        byte[] bytes = new byte[res.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(res.substring(i * 2, i * 2 + 2), 16);
        }
        return Arrays.toString(bytes).replace("[", "").replace("]", "").replace(",", "") + "len:" + bytes.length;
    }

    public static String byteArrayToDecimalString(byte[] byteArray) {
        StringBuilder sb = new StringBuilder();
        for (byte b : byteArray) {
            int decimalValue = b & 0xFF;
            sb.append(decimalValue).append(" ");
        }
        sb.setLength(sb.length() - 1);
        return sb.toString();
    }


    public static String xorHex(String strhex) {
        byte[] bs = parseHexStringToBytes(strhex);
        byte b = bs[0];
        for (int i = 1; i < bs.length; i++) {
            b = (byte) (b ^ bs[i]);
        }

        return Integer.toHexString(b);
    }

    /**
     * 字符串转换为Ascii
     *
     * @param value
     * @return
     */
    public static String stringToAscii(String value) {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                //sbu.append((int) chars[i]).append(",");
                sbu.append(Integer.toHexString(chars[i]));
            } else {
                sbu.append(Integer.toHexString(chars[i]));
            }
        }
        return sbu.toString();

    }

    /**
     * Ascii转换为字符串
     *
     * @param value
     * @return
     */
    public static String asciiToString(String value) {
        StringBuffer sbu = new StringBuffer();
        String[] chars = new String[value.length() / 2];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = value.substring(i * 2, i * 2 + 2);
            sbu.append((char) Integer.parseInt(chars[i], 16));
        }
        return sbu.toString();
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }


    private final static char[] mChars = "0123456789ABCDEF".toCharArray();

    public static String str2HexStr(String str) {
        StringBuilder sb = new StringBuilder();
        byte[] bs = str.getBytes();

        for (int i = 0; i < bs.length; i++) {
            sb.append(mChars[(bs[i] & 0xFF) >> 4]);
            sb.append(mChars[bs[i] & 0x0F]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }

    public static byte[] parseHexStringToBytes(final String hex) {
        // String tmp = hex.substring(2).replaceAll("[^[0-9][a-f]]", "");
        String newValue = hex.toLowerCase(Locale.getDefault());
        String tmp = "";
        if (newValue.substring(0, 2).equals("0x")) {
            tmp = newValue.substring(2);
        } else {
            tmp = newValue;
        }
        // Log.d("wzb","tmp="+tmp);
        byte[] bytes = new byte[tmp.length() / 2]; // every two letters in the
        // string are one byte
        // finally

        String part = "";

        for (int i = 0; i < bytes.length; ++i) {
            part = "0x" + tmp.substring(i * 2, i * 2 + 2);
            bytes[i] = Long.decode(part).byteValue();
            // Log.d("wzb","byte["+i+"]="+bytes[i]);
        }
        // Log.d("wzb","bytes lenght="+bytes.length);
        return bytes;
    }

}